package com.thekernel.youdaoyun.mapper;

import com.thekernel.youdaoyun.bean.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author TheKernel
 * @Date 2019/9/12 7:21 下午
 * @Version 1.0
 * @Description 操作 user 数据库
 */
@Component
@Mapper
public interface UserMapper {

    /**
     * 添加 User
     *
     * @param user 用户信息
     * @return 0: 添加失败 1: 添加成功
     */
    @Insert("INSERT INTO `user`(`username`, `password`, `email`, `user_type`, `status`, `create_time`)" +
            " VALUES(#{username}, #{password}, #{email}, #{userType}, #{status}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    int add(User user);

    /**
     * 根据用户名获取用户id
     *
     * @param username 用户名
     * @return User(user_id)
     */
    @Select("SELECT `user_id` FROM `user` WHERE `username`=#{username}")
    @Results(
            id = "user_id_by_phone",
            value = {
                    @Result(property = "userId", column = "user_id", javaType = Integer.class)
            }
    )
    User queryUserIdByUsername(String username);

    /**
     * 根据电话号码获取用户id
     *
     * @param phoneNumber 电话号
     * @return User(user_id)
     */
    @Select("SELECT `user_id` FROM `user` WHERE `telephone`=#{phoneNumber}")
    @Results(
            id = "user_id",
            value = {
                    @Result(property = "userId", column = "user_id", javaType = Integer.class)
            }
    )
    User queryUserIdByTelephone(String phoneNumber);

    /**
     * 根据用户名获取用户密码(包含user_id, user_type)
     *
     * @param username 用户名
     * @return User(user_id, password, user_type)
     */
    @Select("SELECT `user_id`, `password`, `user_type`, `is_viper` FROM `user` WHERE `username`=#{username} LIMIT 1")
    @Results(
            id = "password",
            value = {
                    @Result(property = "userId", column = "user_id", javaType = long.class),
                    @Result(property = "password", column = "password", javaType = String.class),
                    @Result(property = "userType", column = "user_type", javaType = Integer.class),
                    @Result(property = "isViper", column = "is_viper", javaType = Boolean.class)
            }
    )
    User queryPasswordByUsername(String username);

    /**
     * 用于查看用户信息
     *
     * @param userId 用户 id
     * @return 用户信息
     */
    @Select("SELECT `user_id`, `username`, `nickname`, `avatar`, `sex`, `signature`, `telephone`, `email`, `province`," +
            " `city`, `area`, `user_type`, `status`, `create_time` FROM `user` WHERE `user_id`=#{userId} LIMIT 1")
    @Results(
            id = "user",
            value = {
                    @Result(property = "userId", column = "user_id", javaType = long.class),
                    @Result(property = "username", column = "username", javaType = String.class),
                    @Result(property = "nickname", column = "nickname", javaType = String.class),
                    @Result(property = "avatar", column = "avatar", javaType = String.class),
                    @Result(property = "sex", column = "sex", javaType = Integer.class),
                    @Result(property = "signature", column = "signature", javaType = String.class),
                    @Result(property = "telephone", column = "telephone", javaType = String.class),
                    @Result(property = "email", column = "email", javaType = String.class),
                    @Result(property = "province", column = "province", javaType = String.class),
                    @Result(property = "city", column = "city", javaType = String.class),
                    @Result(property = "area", column = "area", javaType = String.class),
                    @Result(property = "userType", column = "user_type", javaType = Integer.class),
                    @Result(property = "status", column = "status", javaType = Boolean.class),
                    @Result(property = "createTime", column = "create_time", javaType = Date.class)
            }
    )
    User queryUserByUserId(long userId);

    /**
     * 更新用户
     *
     * @param user 用户信息
     * @return 1: 更新成功 0: 更新失败
     */
    @Update("UPDATE `user` SET `nickname`=#{nickname}, `sex`=#{sex}, `signature`=#{signature}, " +
            "`telephone`=#{telephone}, `email`=#{email}, " +
            "`province`=#{province}, `city`=#{city}, `area`=#{area} " +
            "WHERE `user_id`=#{userId} LIMIT 1")
    int updateUser(User user);

    /**
     * 开通会员
     *
     * @param userId 用户id
     * @return 1: 更新成功 0: 更新失败
     */
    @Update("UPDATE `user` SET `is_viper`=1 WHERE `user_id`=#{userId} LIMIT 1")
    int becomeVip(long userId);

    /**
     * 更新用户头像
     *
     * @param userId 用户id
     * @param avatar 用户头像路径
     * @return 1: 更新成功 0: 更新失败
     */
    @Update("UPDATE `user` SET `avatar`=#{avatar} WHERE `user_id`=#{userId} LIMIT 1")
    int updateAvatar(long userId, String avatar);

    /**
     * 修改密码
     *
     * @param user 用户信息
     * @return 1: 更新成功 0: 更新失败
     */
    @Update("UPDATE `user` SET `password`=#{password} WHERE `user_id`=#{userId} LIMIT 1")
    int updatePassword(User user);

}
